﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZYStudios.Controllers;

namespace ZYStudios.Filter
{
    public class DataForMasterPageFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Optional: Work only for GET request
            if (filterContext.RequestContext.HttpContext.Request.RequestType != "GET")
                return;

            // Optional: Do not work with AjaxRequests
            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                return;
            ZYStudios.Models.Data aData = new Models.Data();
            ZYStudios.Models.DataTableAdapters.LinksTableAdapter aLinksAdapter = new Models.DataTableAdapters.LinksTableAdapter();
            aLinksAdapter.Fill(aData.Links);

            List<LinkEditModel> LinksList = new List<LinkEditModel>();
            foreach (var row in aData.Links.ToArray())
            {
                LinksList.Add(new LinkEditModel()
                {
                    ID = row.ID,
                    Link = row.Link,
                    LinkImage = row.LinkImage,
                    LinkName = row.LinkName
                });
            }

            filterContext.Controller.ViewData["Links"] = LinksList;

            
        }
    }
}